Finding unused attributes

I have a customer who'd like to have a function that lists any attributes that aren't used in a module.  I know I could loop through the objects in a module and create a list of attributes that aren't used, but that seems like a lot of work to have to check each object in the module.  Is there an easier way to loop through the attributes and check if they're unused?

 

Chris


Christopher Cote - Thu Apr 25 11:57:11 EDT 2019

Re: Finding unused attributes
Mike.Scharnow - Thu Apr 25 12:15:28 EDT 2019

Well, I would translate "used" with "there is at least one object which has the attribute set to a non-null value", and there are no internal automatic statistics about "usage". So, no, the only way is to loop through all objects (perhaps disregarding deleted objects, but as deleted objects can be undeleted any time, you should check these as well).

 

I would start with a skip list containing all AttrDefs which are ad.object but not ad.system, and when iterating over the objects you remove the attributes that have a value. This way, you can at least stop walking through every single object if your skip list is empty, when all attributes have been used at least once.

 

Edit: On second thought, it might be better not to remove used attributes from the list but to count how often the attributes are used. This way you will notice if there are attributes that are "almost never" used and thus also obsolete.

Re: Finding unused attributes
Christopher Cote - Fri Apr 26 12:29:59 EDT 2019

Mike.Scharnow - Thu Apr 25 12:15:28 EDT 2019

Well, I would translate "used" with "there is at least one object which has the attribute set to a non-null value", and there are no internal automatic statistics about "usage". So, no, the only way is to loop through all objects (perhaps disregarding deleted objects, but as deleted objects can be undeleted any time, you should check these as well).

 

I would start with a skip list containing all AttrDefs which are ad.object but not ad.system, and when iterating over the objects you remove the attributes that have a value. This way, you can at least stop walking through every single object if your skip list is empty, when all attributes have been used at least once.

 

Edit: On second thought, it might be better not to remove used attributes from the list but to count how often the attributes are used. This way you will notice if there are attributes that are "almost never" used and thus also obsolete.

Your translation is correct, and I would only be checking object attributes.  I then want to display the list of unused attributes in a listView to allow the user to delete one or more of the attributes that aren't used.  The issue is that the listView uses an array to fill itself.  So, how do I change the skip list into an array to feed into the listView object or can I just use the skip list as is?

 

Chris

Re: Finding unused attributes
Mike.Scharnow - Fri Apr 26 17:12:14 EDT 2019

Christopher Cote - Fri Apr 26 12:29:59 EDT 2019

Your translation is correct, and I would only be checking object attributes.  I then want to display the list of unused attributes in a listView to allow the user to delete one or more of the attributes that aren't used.  The issue is that the listView uses an array to fill itself.  So, how do I change the skip list into an array to feed into the listView object or can I just use the skip list as is?

 

Chris

At this moment, I'm not sure whether there is a better solution, but this should work:

 

int sizeOfSkip=0
for ad in sk do sizeOfSkip++
string items[sizeOfSkip]
int i=0
for ad in sk do items[i++] = ad.name

 

Re: Finding unused attributes
llandale - Fri Apr 26 20:40:18 EDT 2019

Mike has made a tiny nuance oversight. 

I daresay an object "uses" an attribute if the attribute is set to a non-null value that is neither defaulted nor inherited.  If an object has the default (or inherited) value, then (o.NameAttr "") will return that value, which is non-null.  You want to use this instead:

if (hasSpecificValue(o, ad)) then ad has an assigned value. 

"ad" is type "AttrDef".  Mike' recent other response here: (https://www.ibm.com/developerworks/community/forums/html/topic?id=03233079-9926-4e81-a72a-f4bc3efe41ce&ps=25) has a good example of using "AttrDef" in a way useful for hasSpecificValue.

-Louie

On further nuance, it will not do to figure out what the Default of an attribute is, or what the Inherited value is supposed to be; then retrieve the value and compare it.  That's because you can give a specific value to an obj-attr value that is the same as the default or inherited value.  If you have a Yes/No attribute with default "No", you can still give an obj-attr an actual value of "No".  You cannot tell the difference in the GUI.  In that case, hasSpecificValue will return true, and that I think is what you want.

OK, you CAN tell the difference by temporarily changing the default value (say to "Yes" or nothing), then looking for an actual value.